package com.douqu.game.serverManager;

import com.douqu.game.MainFrame;
import com.douqu.game.MenuManager;
import com.douqu.game.core.factory.LoadFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author: miracle.
 * Description:
 * Date: 2018/8/8 0008 下午 3:54
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class ServerManager extends MenuManager {

    //服务管理button 刷新 一键启动 一键停止
    private JButton startServerBtn, endServerBtn,clearLogBtn;
    //日志
    private static JTextArea serverContentText;

    private java.util.List<ServerColumn> serverColumnList = new ArrayList<>();

    public static final String SERVER_NAME = "server_name_";
    public static final String SERVER_MAIN_BAT = "server_main_bat_";
    public static final String SERVER_BATTLE_BAT = "server_battle_bat_";
    public static final String SERVER_STOP = "server_stop_";

    public ServerManager(Container container, MainFrame mainFrame) {
        super(container, mainFrame);

        initRemark("<html><font color='white'>1.服务器管理工具<br/>" +
                "2.包括服务器的启动，停止<br/>" +
                "3.所有路径不能有中文" +
                "</font></html>",70);
        JLabel serverListTitle = new JLabel();
        serverListTitle.setBounds(20, 90, 120, 30);
        serverListTitle.setFont(MainFrame.font18);
        serverListTitle.setForeground(new Color(0, 0, 0));
        serverListTitle.setText("<html><font color='white'>" +
                "服务器列表:" +
                "</font></html>");
        container.add(serverListTitle);

        startServerBtn = new JButton("一键启动");
        startServerBtn.setBounds(140, 90, 140, 30);
        startServerBtn.setFont(MainFrame.font18);
        container.add(startServerBtn);

        endServerBtn = new JButton("一键停止");
        endServerBtn.setBounds(290,90,140,30);
        endServerBtn.setFont(MainFrame.font18);
        container.add(endServerBtn);

        //加载认证服拉取服务器列表
        int y = loadServers(container,90,35);
        //y是最后一列服务器的纵坐标 加载日志系统
        serverContentText = new JTextArea();
        serverContentText.setFont(new Font("宋体", 0, 16));
        serverContentText.setEditable(false);
        serverContentText.setForeground(Color.WHITE);
        serverContentText.setBorder(null);
        serverContentText.setBackground(new Color(19, 71, 100));

        JScrollPane infoJSP = new JScrollPane(serverContentText);
        infoJSP.setBounds(20, y + 30 + 10, 600, 320);
        container.add(infoJSP);

        clearLogBtn = new JButton("清除日志");
        clearLogBtn.setBounds(20,y + 30 + 10 + 320 + 10,140,35);
        clearLogBtn.setFont(MainFrame.font18);
        container.add(clearLogBtn);

        endServerBtn.addActionListener((event) ->
                        actionListener(event)
        );
        startServerBtn.addActionListener((event) ->
                        actionListener(event)
        );
        clearLogBtn.addActionListener((event) ->
                        serverContentText.setText("")
        );
        initEnd("服务器管理工具");
    }

    private void actionListener(ActionEvent event){
        startActionListener();
        Object source = event.getSource();

        String result = "";
        if(source == startServerBtn)
        {
            startServer();
        }
        else if(source == endServerBtn)
        {
            serverStop();
        }
        endActionListener();
    }

    private int loadServers(Container container,int y,int hight){
//        String ip = null;
//        try{
//            ip = InetAddress.getLocalHost().getHostAddress();
//        }catch (Exception e){
//            e.printStackTrace();
//            System.out.println("获取ip地址出错");
//        }
//        if(StringUtils.isNullOrEmpty(ip)){
//            System.out.println("ip为空 ");
//            return false;
//        }
//        JSONObject result = HttpJsonUtils.httpGet(AUTH_ADDRESS + "?ip=" + ip);
//        if(result == null){
//            System.out.println("请求认证服 获取服务器列表出错");
//            return false;
//        }
//        String resultCode = result.getString("code");
        //if(ReturnMessage.SUCCESS.getCode().equals(resultCode)){
        //JSONArray jsonArray = result.getJSONArray("data");
        //List<ServerModel> serverModelList = jsonArray.toJavaList(ServerModel.class);
        Properties properties = new Properties();
        InputStream input =  null;
        int i = 1;
        try{
            input = LoadFactory.getInputStream("manager.properties");
            properties.load(new InputStreamReader(input,"UTF-8"));
            Object serverName = null;
            Object sreverMainBat = null;
            Object serverBattleBat = null;
            while (true){
                serverName =  properties.get(SERVER_NAME + i);
                sreverMainBat = properties.get(SERVER_MAIN_BAT+i);
                serverBattleBat = properties.get(SERVER_BATTLE_BAT+i);
                if(serverName == null
                        || sreverMainBat == null
                        || serverBattleBat == null){
                    break;
                }
                ServerColumn server = new ServerColumn();
                server.init(i,y + hight * i + 10,container,properties,this);
                serverColumnList.add(server);
                i++;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return y + hight * i + 10;
//        }else{
//            System.out.println("错误码为 " + resultCode);
//            return false;
//        }
    }

    /***
     * 一键启动服务器
     */
    public void startServer(){
        try{
            for(ServerColumn serverColumn:serverColumnList){
                System.out.println("启动主服" + serverColumn.getName());
                serverColumn.startMain(this);
                System.out.println("启动战斗服" + serverColumn.getName());
                serverColumn.startBattle(this);
            }
        }catch (Exception e){
            e.printStackTrace();
            working = false;
        }
    }

    /**
     * 一键关闭服务器
     */
    public void serverStop(){
        try{
            for (ServerColumn serverColumn:serverColumnList){
                System.out.println("停止" + serverColumn.getName());
                serverColumn.stop(this);
            }
        }catch (Exception e){
            e.printStackTrace();
            working = false;
        }
    }


}
